package com.ruoyi.common.utils;

import com.alibaba.fastjson.JSON;
import okhttp3.*;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class OkHttpUtils {

    private static volatile OkHttpUtils okHttpUtils;
    private static OkHttpClient okHttpClient;

    /**
     * 构造初始化
     */
    private OkHttpUtils() {
        /**
         * 构建OkHttpClient
         */
        okHttpClient = new OkHttpClient.Builder()
                /**
                 * 请求的超时时间
                 */
                .readTimeout(300000, TimeUnit.MILLISECONDS)
//                /**
//                 * 设置响应的超时时间
//                 */
//                .writeTimeout(15000, TimeUnit.MILLISECONDS)
//                /**
//                 * 设置连接的超时时间
//                 */
//                .connectTimeout(15000, TimeUnit.MILLISECONDS)
                /**
                 * 构建
                 */
                .build();
    }

    /**
     * 通过单例模式构造对象
     *
     * @return
     */
    public static OkHttpUtils getInstance() {
        if (okHttpUtils == null) {
            synchronized (OkHttpUtils.class) {
                if (okHttpUtils == null) {
                    okHttpUtils = new OkHttpUtils();
                }
            }
        }
        return okHttpUtils;
    }

    /**
     * GET
     * @param url
     * @param params
     * @return
     * @throws IOException
     */
    public Response getRequest(String url, Map<String, Object> params) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .get()
                .build();

        return okHttpClient.newCall(request).execute();
    }

    /**
     * POST
     * @param url
     * @param params
     * @return
     * @throws IOException
     */
    public Response postRequest(String url, Map<String, Object> params) throws IOException {
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody requestBody = RequestBody.create(mediaType, null == params ? null : JSON.toJSONString(params));
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();

        return okHttpClient.newCall(request).execute();
    }

    /**
     * POST
     * @param url
     * @param requestBody
     * @return
     * @throws IOException
     */
    public Response postRequest(String url, RequestBody requestBody) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();

        return okHttpClient.newCall(request).execute();
    }

    /**
     * POST body body为String
     * @param url
     * @param params
     * @return
     * @throws IOException
     * @author wlf
     * @date 2020-03-26 09:48
     */
    public Response postRequest(String url, String params) throws IOException {
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType, params);
        Request request = new Request.Builder()
                .url(url)
                .method("POST", body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .build();
        return okHttpClient.newCall(request).execute();
    }
}
